var productTypeArray = ['2','3','4','1','6','7'];
var productType = productTypeArray[0];
var index = '0';
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	
}
//初始化绑定事件
function initButton(){
	//绑定搜索分类按钮
	$('.nav-box li').on('click',function() {
		index = $(this).index();
		$(this).addClass('nav-sel').siblings().removeClass('nav-sel');
		$('.change-item').eq(index).addClass('dis-block').siblings().removeClass('dis-block');
		search();
	});
	
	//绑定取消按钮
	$('.cancel').on('click',function(){
		var oldPath = localStorage.getItem('oldPath');
		window.location = server_url + oldPath;
	});
	
	//绑定搜索按钮
	$('body').on('click','.search-btn',function(){
		search();
	});
	
	//给搜索结果绑定事件
	$('body').on('click','.skill-list',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	$('body').on('click','.pro-list',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	$('body').on('click','.list-item',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	$('body').on('click','.inf-list',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
}
//搜索事件
function search(){
	productType = productTypeArray[index];
	var htmlType = $('.change-item').eq(index);
	
	var search = $('.search-text').val();
	
	//htmlType.html(search);
	
	if(search==''){
		//如果没有搜索条件，无响应
		alert('请输入搜索条件');
		return ;
	}else{
		//向后台发送请求，要求是搜索条件加类型
		$.ajax({
			type : "post",
			url : server_url
					+ 'rest/tea/product/searchProduct.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				token : token,
				type:productType,
				searchCondtion:search
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					var str = '';
					var productList = result.data;
					if(index=='0'){
						$.each(productList,function(i,n){
							//var sex = n.
							var age = n.teaAge;
							var year = n.teaYear;
							var trip;
							if(n.teaTrip=='0'){
								trip = '不出差';
							}else{
								trip = '可出差';
							}
							str +='<div class="skill-list clear " type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="skill-box fr"><div class="item-det clear"><span class="fl name">'+n.productname+'</span><span class="fl color02">茶艺师</span><span class="fl color03">评茶师</span></div><div class="item-time2 clear"><span class="fl">女/'+n.teaAge+'岁/从业'+n.teaYear+'年/'+trip+'</span></div><div class="item-time2 clear"><span class="fl">成交量：'+n.teaSellNum+'</span><span class="fr">月薪面议</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span>元</div><p class="fr">'+n.teaLocation+'</p></div></div></div>';
						});
					}else if(index=='1'){
						$.each(productList,function(i,n){
							var nowStr;
							if(n.teaPurAd=='0'){
								nowStr= '<span class="fl green-box	">现购中</span>';
							}else{
								nowStr = '<span class="fl red-box	">预购中</span>';
							}
							str+='<div class="pro-list clear" type="'+n.cateid+'" productid="'+n.productid+'"><div class="fl pro-item"><img src="'+n.pic+'" alt=""><div class="name-box clear">'+nowStr+'<p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div>';
						});
					}else if(index=='2'){
						$.each(productList,function(i,n){
							var nowStr;
							if(n.teaPurAd=='0'){
								nowStr= '<span class="fl green-box	">现购中</span>';
							}else{
								nowStr = '<span class="fl red-box	">预购中</span>';
							}
							str+='<div class="pro-list clear" type="'+n.cateid+'" productid="'+n.productid+'"><div class="fl pro-item"><img src="'+n.pic+'" alt=""><div class="name-box clear">'+nowStr+'<p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div>';
						});
					}else if(index=='3'){
						$.each(productList,function(i,n){
							str+='<div class="skill-list clear" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="skill-box fr"><div class="item-det clear"><span class="fl name">'+n.productname+'</span></div><div class="num-box2 clear"><p class="fl">'+n.teaLocation+'</p></div><div class="club-phone-box"><p class="club-phone">'+n.teaPhone+'</p></div><div class="num-box"><div class="fl"><span>'+n.teaPrice+'</span>元/人</div></div></div></div>';
						});
					}else if(index=='4'){
						$.each(productList,function(i,n){
							str+='<div class="list-item clear" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="item-r fl"><p class="item-tit">'+n.productname+'</p><div class="item-det clear"><span class="fl color03">已认证</span></div><div class="item-time clear"><p class="fl">'+n.teaArea+'亩</p><span class="fr">'+n.limitTime+'年</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span>万元</div><p class="fr">'+n.teaLocation+'</p></div></div></div>';
						});
					}else if(index=='5'){
						$.each(productList,function(i,n){
							str+='<div class="inf-list clear" type="'+n.cateid+'" productid="'+n.productid+'"><div class="inf-l fl"><p class="">'+n.productname+'</p><div class="time-box clear"><span class="num-s fl">'+n.teaSellNum+'次</span><span class="time-s fr">'+n.addtime+'</span></div></div><div class="inf-r fr"><img src="'+t.pic+'" alt=""></div></div>';
						});
					}
					htmlType.html(str);
				}else{
					showErrorMsg(result.data);
				}
			}
		});
	}
}